package com.ruoyi.web.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.liangshi.domain.ArchItem;
import com.ruoyi.liangshi.domain.GdArchPoint;
import com.ruoyi.liangshi.service.IArchItemService;
import com.ruoyi.liangshi.service.IGdArchPointService;
import com.ruoyi.web.dto.ArchItemDto;
import com.ruoyi.web.dto.ArchPointDto;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.logging.Logger;


@Service
@AllArgsConstructor
public class ArchReceiveConsumer {

    private final String DATA_PUSH_EXCHANGE = "data.sender.exchange";
    private final String DATA_ARCH_ITEM_TOPIC = "data.sender.arch.item.topic";
    private final String DATA_ARCH_POINT_TOPIC = "data.sender.arch.point.topic";

    private  final IArchItemService archItemService ;

    private  final IGdArchPointService  gdArchPointService ;


    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = DATA_ARCH_ITEM_TOPIC, autoDelete = "false", durable = "true"),
            exchange = @Exchange(value = DATA_PUSH_EXCHANGE, durable = "true", type = ExchangeTypes.TOPIC),
            key = (DATA_ARCH_ITEM_TOPIC))})
    private void archItemDataReceiver(String message) {
        if(true){
            return;
        }
        Logger logger = Logger.getLogger("ArchReceiveConsumer");
        Object object = JSON.parse(message);
        if (object instanceof JSONArray) {
            logger.info("ArchItemDto 是数组对象信息" );
            return ;
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(message);
        ArchItemDto archItemDto =jsonObject.toJavaObject(ArchItemDto.class);
        Long id = archItemDto.getId(); ;
        ArchItem archItem = archItemService.getById(id);
        if(ObjectUtil.isNull(archItem)){
            archItem = new ArchItem() ;
        }
        BeanUtil.copyProperties(archItemDto,archItem);
        archItem.setIsEnable(archItemDto.getIsEnable()?"t":"f");
        archItemService.saveOrUpdate(archItem);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = DATA_ARCH_POINT_TOPIC, autoDelete = "false", durable = "true"),
            exchange = @Exchange(value = DATA_PUSH_EXCHANGE, durable = "true", type = ExchangeTypes.TOPIC),
            key = (DATA_ARCH_POINT_TOPIC))})
    private void archPointDataReceiver(String message) {
        if(true){
            return;
        }
        Logger logger = Logger.getLogger("ArchReceiveConsumer");
        Object object = JSON.parse(message);
        if (object instanceof JSONArray) {
            logger.info("archPointDto"+ message);
            return ;
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(message);

        ArchPointDto archPointDto =jsonObject.toJavaObject(ArchPointDto.class);
        Long id = archPointDto.getId(); ;
        GdArchPoint gdArchPoint = gdArchPointService.selectGdArchPointById(id);
        if(ObjectUtil.isNull(gdArchPoint)){
            gdArchPoint = new GdArchPoint() ;
        }
        BeanUtil.copyProperties(archPointDto,gdArchPoint);
        gdArchPoint.setIsEnable(archPointDto.getIsEnable()?"t":"f");
        gdArchPoint.setCreateShopId(archPointDto.getBelongShopId());
        gdArchPointService.saveOrUpdate(gdArchPoint);

    }

}
